import requests
import random
import time

from settings import headers_list, cookies, stock_day_line_url
from db import stock_db, data_day_db


def send_req(session, days, url, code, proxies, timeout=5):
    
    session.headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "no-cache",
        "cookie": cookies[0],
        "origin": "https://xueqiu.com",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://xueqiu.com/S/SH601360?md5__1038=n4IxRD0DyDcD2i7KDsAET1D9BCSPDt3Wt4D",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"macOS\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": random.choice(headers_list)
    }   
    try:
        if proxies.get("http"):
            res = session.get(url, proxies=proxies, timeout=timeout)
        else:
            res = session.get(url, timeout=timeout)
        data = res.json()
        if res.status_code != 200 or not res.json() or not data.get("data") :
            return {"ok": 0, "data": code}
        return {"ok": 1, "data": data["data"]}
            
    except Exception as e:
        # print("failed", code, proxies)
        return {"ok": 0, "data": code}


def parse_klines(klines):
    ret = []
    for line in klines:
        split_line = line.split(",")
        # print(split_line)
        temp = {
            "date": split_line[0],
            "start_price": split_line[1],
            "end_price": split_line[2],
            "high_price": split_line[3],
            "low_price": split_line[4],
            "all_hands": split_line[5],
            "all_money": split_line[6],
            "other1": split_line[7],
            "other2": split_line[8],
            "other3": split_line[9],
            "other4": split_line[10],
            "other5": split_line[11],
            "other6": split_line[12],
            "other7": split_line[13],
        }
        ret.append(temp)
    return ret


def get_day_kline():
    stocks = stock_db.find({"exist": 1})
    session = requests.Session()
    days = 250
    count1 = 0
    count2 = 0
    for stock in stocks:
        code_prifix = "0."
        if stock["prefix"] == "SZ":
            code_prifix = "1."
        # proxy = random.choice(ips_list)
        klines = send_req(session, days, stock_day_line_url % (code_prifix+stock["code"], days),
                          code_prifix+stock["code"], proxy)
        count1 += 1

        if klines.get("ok") > 0:
            data = parse_klines(klines)
            count2 += 1
            print(stock["code"], count1, count2)
            data_day_db.update_one({'symbol': stock['symbol']}, {
                "$set": {"item": data}}, upsert=True)
            time.sleep(0.1)


if __name__ == "__main__":
    get_day_kline()
